package com.ubst.smartad.impl;

import java.sql.*;
import java.util.*;
import java.io.*;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import com.ubst.smartad.dao.TSASessionDO;
import com.ubst.smartad.common.TimeUtil;
import com.ubst.smartad.logger.Debug;

public class TSASessionMngImpl {
	
	Connection con = null;
	private String serverName;
	private Debug debug;

	/** 
     * 생성자
     *   
     *   
    */ 
	public TSASessionMngImpl(Connection con, String serverName, String log_level) {
		this.con = con;
		this.serverName = serverName;
		this.debug = new Debug("TSASessionMngImpl", log_level);

	}

    /** 
	 * @author KJH
     * 계정관리 로그인 세션등록
     *   
     */  
    public void insertSession(TSASessionDO sessionDO) throws SQLException{
        PreparedStatement pstmt=null;

        StringBuffer queryBuf = null;
        try {

			long l_cur_date = TimeUtil.getCurrentTime();

            queryBuf = new StringBuffer();
			queryBuf.append("INSERT INTO TSA_SESSION " )
				.append(" ( ")
				.append("  USERID ,SESSIONID ,REMOTEIP ,NSTATUS ,CONNDATE ")
				.append(" ) ")
				.append(" VALUES")
				.append(" ( ") 
				.append("  ? ,? ,? ,? ,? ")
				.append(" ) ");

			debug.debug("insertSession ==> "+queryBuf.toString());

			pstmt = con.prepareStatement( queryBuf.toString() );
			int idx = 1;
			pstmt.setString(idx++, sessionDO.userid);
			pstmt.setString(idx++, sessionDO.sessionid);
			pstmt.setString(idx++, sessionDO.remoteip);
			pstmt.setInt(idx++, sessionDO.nstatus);
			pstmt.setString(idx++, TimeUtil.getDateString(l_cur_date, "yyyyMMddHHmmss"));

            pstmt.execute();
            pstmt.close();
			queryBuf = null;

        }catch (SQLException e){
			throw e;
        }finally{
            if ( pstmt != null) try{pstmt.close();}catch(SQLException ee){}
        }

    }
	
    /** 
	 * @author KJH
     * 계정관리 로그인 세션정보변경
     *   
     */  
    public void updateSession(TSASessionDO sessionDO) throws SQLException{
        PreparedStatement pstmt=null;

        StringBuffer queryBuf = null;
        try {

			long l_cur_date = TimeUtil.getCurrentTime();

            queryBuf = new StringBuffer();
			queryBuf.append("UPDATE TSA_SESSION " )
				.append(" SET ")
				.append(" SESSIONID=? ,REMOTEIP=? ,NSTATUS=? ,CONNDATE=? ")
				.append(" WHERE USERID=? ");

			debug.debug("updateSession ==> "+queryBuf.toString());

			pstmt = con.prepareStatement( queryBuf.toString() );
			int idx = 1;
			pstmt.setString(idx++, sessionDO.sessionid);
			pstmt.setString(idx++, sessionDO.remoteip);
			pstmt.setInt(idx++, sessionDO.nstatus);
			pstmt.setString(idx++, TimeUtil.getDateString(l_cur_date, "yyyyMMddHHmmss"));
			pstmt.setString(idx++, sessionDO.userid);

            pstmt.execute();
            pstmt.close();
			queryBuf = null;

        }catch (SQLException e){
			throw e;
        }finally{
            if ( pstmt != null) try{pstmt.close();}catch(SQLException ee){}
        }

    }

    /** 
	 * @author KJH
     * 계정관리 로그인 세션 시간변경
     *   
     */  
    public void updateSessionTime(String userid) throws SQLException{
        PreparedStatement pstmt=null;

        StringBuffer queryBuf = null;
        try {

			long l_cur_date = TimeUtil.getCurrentTime();

            queryBuf = new StringBuffer();
			queryBuf.append("UPDATE TSA_SESSION " )
				.append(" SET ")
				.append(" CONNDATE=? ")
				.append(" WHERE USERID=? ");

			debug.debug("updateSessionTime ==> "+queryBuf.toString());

			pstmt = con.prepareStatement( queryBuf.toString() );
			int idx = 1;
			pstmt.setString(idx++, TimeUtil.getDateString(l_cur_date, "yyyyMMddHHmmss"));
			pstmt.setString(idx++, userid);

            pstmt.execute();
            pstmt.close();
			queryBuf = null;

        }catch (SQLException e){
			throw e;
        }finally{
            if ( pstmt != null) try{pstmt.close();}catch(SQLException ee){}
        }

    }

	/**
	 * @author : KJH 
	 * 계정관리 로그인 세션등록
	 *
	 */
	public TSASessionDO selectSession(String userid) throws SQLException {

		PreparedStatement pstmt=null;
		ResultSet rs = null;

		TSASessionDO sessionDO = null;

		try {
			
			StringBuffer queryBuf = new StringBuffer();
			queryBuf.append("SELECT ")
				.append("  SESSIONID ,REMOTEIP ,NSTATUS ,CONNDATE ")
				.append(" FROM TSA_SESSION ")
				.append(" WHERE USERID=? ");

			debug.debug("selectSession ==> "+queryBuf.toString());

			pstmt = con.prepareStatement( queryBuf.toString() );
			int idx = 1;
			pstmt.setString(idx++, userid);

			rs = pstmt.executeQuery();

			if ( rs.next() ){
				sessionDO = new TSASessionDO();
				sessionDO.userid = userid;
				sessionDO.sessionid = rs.getString("SESSIONID");
				sessionDO.remoteip = rs.getString("REMOTEIP");
				sessionDO.nstatus = rs.getInt("NSTATUS");
				sessionDO.conndate = rs.getString("CONNDATE");
			}

			rs.close();
			pstmt.close();

		}catch (SQLException e){
			throw e;
		}finally{
			if ( rs != null) try{rs.close();}catch(SQLException ee){}
			if ( pstmt != null) try{pstmt.close();}catch(SQLException ee){}
		}

		return sessionDO;
	}

    /** 
	 * @author KJH
     * 계정관리 로그인 세션 삭제
     *   
     */  
    public void deleteSession(String userid) throws SQLException{
        PreparedStatement pstmt=null;

        StringBuffer queryBuf = null;
        try {

			long l_cur_date = TimeUtil.getCurrentTime();

            queryBuf = new StringBuffer();
			queryBuf.append("DELETE FROM TSA_SESSION " )
				.append(" WHERE USERID=? ");

			debug.debug("deleteSession ==> "+queryBuf.toString());

			pstmt = con.prepareStatement( queryBuf.toString() );
			int idx = 1;
			pstmt.setString(idx++, userid);

            pstmt.execute();
            pstmt.close();
			queryBuf = null;

        }catch (SQLException e){
			throw e;
        }finally{
            if ( pstmt != null) try{pstmt.close();}catch(SQLException ee){}
        }

    }


}
